#ifndef WXCAMVIEWWINDOW_H_
#define WXCAMVIEWWINDOW_H_




#include <wx/wx.h>
#include <cvaux.h>
#include <cv.h>
#include <highgui.h>
#include "../vcCodec.h"
#include <iostream>

 

using namespace std;

class MainFrame;

class Camera;
/**
 * \brief wxWindow in charge of showing the image of the application.
 * 
 * Inherits from wxWindow (see wxwidgets doc) and vcCodec. wxCamViewWindow owns a Camera, from which it will
 * take images and will show on itself. It therefore owns two kind of image formats, IplImage and wxBitmap, 
 * which are called <b>cvImg</b> and <b>wxImg</b>, respectively. Although the two contains the same image, the first one 
 * is used for image processing due to the use of opencv library. The other one is used for displaying the 
 * image in the window. On this image figures will
 * be drawn, for example when zooming, a rectangle will be drawn to show the selected area.
 * The image can be zoomed in or out, and color filtering can be applied to it, thanks to the methods 
 * implemented invccodec There are some important flags: 
 * <ul>
 * <li> <i>selectEnable:</i> Defines wether it is allowed to do a selection on the image or not.
 * <li> <i>selecting:</i> Defines wether if a selection is taking place or not.
 * <li> <i>filterOn:</i> Defines wether a color filter will be applied or not.
 * </ul>
 * 
 *  
 
 */
class wxCamViewWindow : public wxWindow, public vcCodec
{

private:

	
	
	
	MainFrame* parent;
	/**
	 * \brief Camera element from which the images will be taken.
	 */
	Camera *camera;
	
	/**
	 * \brief Image to be shown on the window
	 */
	wxBitmap wxImg;
	
	/**
	 * \brief Image in IplFormat for vision processing.
	 */
	IplImage* cvImg; 
	 
	/**
	 * \brief First X coordinate of rectangle to be drawn on zooming.
	 */
	long initRectX;
	/**
	 * \brief First Y coordinate of rectangle to be drawn on zooming.
	 */
	long initRectY;
	
	/**
	 * \brief Second X coordinate of rectangle to be drawn on zooming.
	 */
	long finalRectX;
	
	/**
	 * \brief Second Y coordinate of rectangle to be drawn on zooming.
	 */
	long finalRectY;
	
	/**
	 * \brief Flag indicating possibility of selecting areas in window.
	 */
	bool selectEnable;
	
	/**
	 * \brief Flag indicating if area selection is currently taking place in window.
	 */
	bool selecting;
	
	/**
	 * \brief Flag indicating if color filter is on.
	 */
	bool filterOn;
	
	
	/**
	 * \brief Flag indicating if axes should be painted on iamge.
	 */
	bool isAxesEnabled;
	
	/**
	 * \brief Flag indicating if area of proyection was (re)defined.
	 */
	bool proyectPlaneReady;
	
		
public:

	/**
	 * \brief Constructor of the Class 
	 * 
	 * \param frame The parent window 
	 * \param pos The initial position of the window
	 * \param size The size of the window
	 * 	 
	 */
	wxCamViewWindow(wxWindow *frame, const wxPoint& pos, const wxSize& size );
	
	/**
	 * \brief Destructor of the Class
	 */
	~wxCamViewWindow();
	
	/**
	 * \brief Updates the window with the current wxImg
	 * \param dc wxPaintDC The Device context for painting (see wxwidgets doc) 
	 */
	void Draw(wxPaintDC &dc );
	
	/**
	 * \brief updates the image by taking a new shoot from the camera.
	 * 
	 * This function takes a shoot of the already initialized camera. The format of the
	 * image returned from camera is IplImage and in BGR coding, so this function also converts it 
	 * to RGB coding, and calls  updateImg.
	 */
	 
	void getCamImage();
	
	/**
	 * \brief Initializes the camera, in order to be able to take images from it.
	 * \param camID the ID of the camera to be initialized. In linux it corresponds to the 
	 * index of /dev/videoX to be used.
	 */
	void initCam(int camID, char* camSrc,int camWidth,int camHeight);
	
	/**
	 * \brief Releases the camera that was initialized, in order to free the respective hardware and 
	 * memory utilized.
	 */
	void releaseCam();
	
	/**
	 * \brief returns the selectEnable flag.
	 * 
	 * The selectEnable flag informs wether zones in the window can be selected. It is used on zooming
	 * issues.
	 * 
	 * \return false or true.
	 */
	bool isSelectEnable();
	
	/**
	 * \brief sets the state of the selectEnable flag.
	 * 
	 * \param selectEnable the new state of the flag
	 */
	void setSelectEnable(bool selectEnable);
	
	/**
	 * \brief updates the image showed in the window, and applies color filter if desired.
	 * 
	 * This function converts the IplImage to wxImage, so it can be painted in the window. It uses
	 * cvImg2wxBitmap from vcCodec. If filterOn flag is set, it applies a colorFilter, using the 
	 * index defined in MainFrame.
	 */
	void updateImg();
	
	/**
	 * \brief Resets the ROI of the image.
	 * 
	 * If a ROI (Region of interest) is set in the image, mostly used in zooming issues, it will be 
	 * reset. 
	 */
	void resetImg();
	
	/**
	 * \brief Sets the camera used by the window.
	 * 
	 * \param camera The camera to be used by the class.
	 */ 
	void setCam(Camera* camera);
	
	/**
	 * \brief Returns the filterOn flag.
	 * 
	 * Returns the state of the filterOn flag, which defines wether a color filter has to be 
	 * applied when showing images or not.
	 * 
	 * \return false or true.
	 */
	bool isFilterOn();
	
	/**
	 * \brief sets the state of the filterOn flag.
	 * 
	 * \param state the new state of the flag
	 */
	void setFilterOn(bool state);
	
	/**
	 * \brief returns the pointer to cvImg (openCv Image)
	 * 
	 */
	IplImage* getCvImage();
	
	
	/**
	 * \brief Used to show axes on the image.
	 * 
	 * This function is mainly used when the area of work has to be marked. It helps the user
	 * to identify the areas in the image where he can define each point of the polygon that finally
	 * defines the work area for the experiment.
	 */
	void traceAxes(bool state);
	
	/**
	 * \brief Defines if the proyect plane is already defined.
	 */
	bool isProyectPlaneReady();
	

		
private:

	/**
	 * \brief Called when left button of mouse is pressed.
	 * 
	 * It calculates the coordinates of the point where the mouse pointer was, when the
	 * left button was pressed. It also permits the drawing of the rectangle used to signal the 
	 * area to be zoomed, by setting the selecting flag. This function is executed, only if selectEnable
	 * flag is set. 
	 * 
	 * \param event The mouse event (see wxwidgets doc).
	 */
	void OnLeftDown( wxMouseEvent& event );
	
	/**
	 * \brief Called when left button of mouse is released.
	 * 
	 * It calculates the coordinates of the point where the mouse pointer was, when the
	 * left button was released. With these two points, where the mouse left button was pressed and released,
	 * the ROI (Region of Interest) of the image will be set. The function cvImg2wxBitmap from vcCodec will
	 *  be called. This function is executed only if selectEnable 
	 * and selecting flags are set.
	 * 
	 * \param event The mouse event (see wxwidgets doc).
	 */	
	void OnLeftUp( wxMouseEvent& event );
	
	/**
	 * \brief Called when the mouse is moving.
	 * 
	 * It draws a rectangle on the image, from the point where the mouse left button was pressed, to
	 * the actual point of the mouse pointer. The rectangle is filled with transparent brush, and the
	 * line is a short dashed one.
	 * 
	 * \param event The mouse event (see wxwidgets doc).
	 */	  
	void OnMove( wxMouseEvent& event );
	
	/**
	 * \brief Called when a paint event is called on the window.
	 * 
	 * This function generates a wxPainDC for drawing purposes (in this case drawing the image of the camera).
	 * Draw function is called.
	 * 
	 * \param event The paint event (see wxwidgets doc).
	 */	
	void OnPaint( wxPaintEvent& event );
	
	/**
	 * \brief Called when a size event is called on the window.
	 * 
	 * This function resizes the wxImg, so it fits the new size of the window.
	 * 
	 * \param event The paint event (see wxwidgets doc).
	 */	
	void OnSize( wxSizeEvent& event );
	
	
	
};
#endif /*WXCAMVIEWWINDOW_H_*/
